package com.keliangliu.demo.elk;

import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: $
 * @Author: qxd
 * @date: $
 */
@Service
public class ElkServiceBiz {
//
//    @Autowired
//    private ElasticsearchRestTemplate esTemplate;

//    @Autowired
//    private IndexOperations indexOperations;

    @Autowired
    private ItemRepository itemRepository;

//    public void createIndex() {
//        // 创建索引，会根据Item类的@Document注解信息来创建
//        indexOperations.create();
//        // 配置映射，会根据Item类中的id、Field等字段来自动完成映射
//        indexOperations.createMapping(Item.class);
//    }
//
//
//    public void deleteIndex() {
//        indexOperations.delete();
//        // 根据索引名字删除
//        //esTemplate.deleteIndex("item1");
//    }

    public void indexList() {
        List<Item> list = new ArrayList<>();
        list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.baidu.com/13123.jpg"));
        list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.baidu.com/13123.jpg"));
        // 接收对象集合，实现批量新增
        itemRepository.saveAll(list);
    }

    public void indexInsert() {
        Item item = new Item(1L, "小米手机7", " 手机",
                "小米", 3499.00, "http://image.baidu.com/13123.jpg");
        itemRepository.save(item);
    }
    /**
     * 修改和新增是同一个接口，区分的依据就是id。
     */
    public void indexUpdate(){
        Item item = new Item(1L, "苹果XSMax", " 手机",
                "小米", 3499.00, "http://image.baidu.com/13123.jpg");
        itemRepository.save(item);
    }

    /**
     * match底层是词条匹配
     */

    public void testMathcQuery(){
        // 查询条件生成器
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchQuery("title","小米"));

        // 查询 自动分页 ,默认查找第一页的10条数据
        Page<Item> list = this.itemRepository.search(builder.build());

        for(Item item:list){
            System.out.println(item);
        }

    }



    public void testTermQuery(){

        // 查询条件生成器
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.termQuery("title","小米"));

        // 查询 自动分页 ,默认查找第一页的10条数据
        Page<Item> list = this.itemRepository.search(builder.build());

        for(Item item:list){
            System.out.println(item);
        }


    }



    public void testFuzzyQuery(){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.fuzzyQuery("title","faceoooo"));

        Page<Item> list = this.itemRepository.search(builder.build());
        for(Item item:list){
            System.out.println(item);
        }


    }



    public void testBooleanQuery(){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title","手机"))
                .must(QueryBuilders.termQuery("brand","小米"))
        );

        Page<Item> list = this.itemRepository.search(builder.build());
        for(Item item:list){
            System.out.println(item);
        }


    }

    public void testRangeQuery(){
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //        queryBuilder.withQuery(QueryBuilders.fuzzyQuery("title","小目"));

        queryBuilder.withQuery(QueryBuilders.rangeQuery("price").from(3000).to(4000));

        Page<Item> page = itemRepository.search(queryBuilder.build());

        for(Item i:page){
            System.out.println(i);
        }


    }




}
